package com.qf.lanzhou.shop.api.controller;

import com.qf.lanzhou.shop.pojo.entity.User;

import com.qf.lanzhou.shop.api.service.LoginphoneService;
import com.qf.lanzhou.shop.pojo.vo.BaseRespVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @Author jian
 * @Date 2021/1/9 10:27
 * @JDK 1.8
 * @Version 1.0
 */
@Controller
public class LoginphoneController extends BaseController {

    @Autowired
    private LoginphoneService loginphoneService;


    @ApiOperation(value = "手机号登录接口")
    @PostMapping(value = "/loginphone")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "phone", value = "手机号",
                    required = true, dataType = "String")
    })
    public BaseRespVO loginphone(String phone, HttpServletRequest req) {
        User user = loginphoneService.selectphone(phone);
        if (user == null) {
            return convertErrorResult("用户不存在,请注册");
        }
        //把用户信息存到session中

        String phone1 = user.getPhone();
        req.getSession().setAttribute("phone1", phone1);
        //生成验证码
        String code = UUID.randomUUID().toString().substring(0, 4);
        User user1 = new User();
        user1.setCode(code);

        //把生成的验证码存在session中
        req.getSession().setAttribute("user1", user1);

        boolean b = loginphoneService.send(phone, "SMS_207962154", user1);
        if (b) {
            //发送成功
            System.out.println("发送成功");
            return convertSuccessResult(user);
        } else {
            //发送失败
            System.out.println("发送失败");
            return convertErrorResult("获取验证码失败");
        }

    }

    @ApiOperation(value = "手机号登录接口2")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "phone", value = "手机号",
                    required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "code", value = "验证码",
                    required = true, dataType = "String")
    })
    @PostMapping(value = "/login")
    public BaseRespVO login(String phone, String code, HttpServletRequest req) {

        Object phone1 = req.getSession().getAttribute("phone1");
        User user1 = (User) req.getSession().getAttribute("user1");
        String code1 = user1.getCode();

        if (!phone1.equals(phone)) {
            System.out.println("手机号码不正确");
            return convertErrorResult("手机号码不正确");
        }
        if (!code1.equals(code)) {
            System.out.println("验证码不正确");
            return convertErrorResult("验证码不正确");
        }

       User user = loginphoneService.selectmessage(phone);
        int count = (int) req.getServletContext().getAttribute("count");
        count=count+1;
        req.getServletContext().setAttribute("count",count);
       req.getSession().setAttribute("user", user);

        User user2 = (User) req.getSession().getAttribute("user");
        String phone2 = user2.getPhone();
        User loginTime=loginphoneService.selectlogintime(phone2);
        if(loginTime==null){
          return convertErrorResult("用户不存在");
        }else {
            int k=loginphoneService.update(phone2);
            if(k==1){
               User user3=loginphoneService.selectallagain(phone2);
               req.getSession().setAttribute("user",user3);
            }
            return convertSuccessResult("100");
        }

    }
}
